{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class DB:\n",
    "    def __init__(self,host,user,password,database,cursorclass=None):\n",
    "        self.host=host\n",
    "        self.user=user\n",
    "        self.password=password\n",
    "        self.database=database\n",
    "        self.cursorclass=cursorclass\n",
    "    def connect(self):\n",
    "        import pymysql\n",
    "        self.db=pymysql.connect(host=self.host,user=self.user,password=self.password,database=self.database,\n",
    "                               cursorclass=self.cursorclass)\n",
    "        self.cursor=self.db.cursor()\n",
    "        \n",
    "    def get_one(self,sql):\n",
    "        result=0\n",
    "        try:\n",
    "            self.connect()\n",
    "            self.cursor.execute(sql)\n",
    "            result=self.cursor.fetchone()\n",
    "            self.close()\n",
    "        except Exception as e:\n",
    "            print('select error ',e)\n",
    "        return result\n",
    "    def get_all(self,sql):\n",
    "        result=0\n",
    "        try:\n",
    "            self.connect()\n",
    "            self.cursor.execute(sql)\n",
    "            result=self.cursor.fetchall()\n",
    "            self.close()\n",
    "        except Exception as e:\n",
    "            print('select error',e)\n",
    "        return result\n",
    "    def __edit(self,sql):\n",
    "        result=0\n",
    "        try:\n",
    "            self.connect()\n",
    "            self.cursor.execute(sql)\n",
    "            self.db.commit()\n",
    "        except Exception as e:\n",
    "            print('select error',e)\n",
    "        return result\n",
    "    def insert(self,sql):\n",
    "        return self.__edit(sql)\n",
    "    def update(self,sql):\n",
    "        return self.__edit(sql)\n",
    "    def close(self):\n",
    "        self.cursor.close()\n",
    "        self.db.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
